package service

import (
	"github.com/gin-gonic/gin"
	"guns-go/global"
	"guns-go/model"
	"guns-go/model/request"
	"guns-go/model/response"
	"guns-go/utils"
)

func SysSmsPage(sysSmsInfoParam *request.SysSmsInfoParam, c *gin.Context) (err error, result *response.PageResult) {

	var sysSmsSlice []*model.SysSms
	db := global.GVA_DB

	if sysSmsInfoParam != nil {

		//根据手机号模糊查询
		if sysSmsInfoParam.PhoneNumbers != "" {
			db = db.Where("phone_numbers like ?", "%"+sysSmsInfoParam.PhoneNumbers+"%")
		}

		//根据发送状态查询（字典 0 未发送，1 发送成功，2 发送失败，3 失效）
		if sysSmsInfoParam.Status != nil {
			db = db.Where("status = ?", sysSmsInfoParam.Status)
		}

		//根据来源查询（字典 1 app， 2 pc， 3 其他）
		if sysSmsInfoParam.Source != nil {
			db = db.Where("source = ?", sysSmsInfoParam.Source)
		}
	}

	var totalCount int64

	err = db.Find(&sysSmsSlice).Count(&totalCount).Error

	if err != nil {
		return err, nil
	}

	pageNo, pageSize, db := utils.DefaultPage(c, db)

	err = db.Find(&sysSmsSlice).Error

	if err != nil {
		return err, nil
	}

	return err, utils.CoverToPageResult(pageNo, pageSize, totalCount, sysSmsSlice)
}
